<?php

/**
 * This is the model class for table "nomenclador.prestaciones".
 *
 * The followings are the available columns in table 'nomenclador.prestaciones':
 * @property integer $id
 * @property string $codigo
 * @property string $tema
 * @property string $descripcion
 * @property string $precio
 * @property integer $dias_uti
 * @property integer $dias_sala
 * @property integer $dias_totales
 * @property integer $dias_max
 * @property string $maternidad
 * @property integer $id_categoriaprestacion
 *
 * The followings are the available model relations:
 * @property PrestacionesPermitidas[] $prestacionesPermitidases
 * @property CategoriaPrestaciones $idCategoriaprestacion
 */
class Prestaciones extends CActiveRecord
{
    
    public $alta_complejidad;
    
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'nomenclador.prestaciones';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('codigo, tema, descripcion, precio', 'required'),
			array('dias_uti, dias_sala, dias_totales, dias_max, id_categoriaprestacion', 'numerical', 'integerOnly'=>true),
			array('codigo', 'length', 'max'=>30),
			array('tema', 'length', 'max'=>100),
                        array('precio','safe'),
                        array('precio','rulesPrecioValido'), 
			array('descripcion', 'length', 'max'=>250),
                        array('alta_complejidad', 'length', 'max'=>1), 
			array('precio', 'length', 'max'=>8),
			array('maternidad', 'length', 'max'=>150),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, codigo, tema, descripcion, precio, dias_uti, dias_sala, dias_totales, dias_max, maternidad, id_categoriaprestacion', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'MisPrestacionesPermitidas' => array(self::HAS_MANY, 'PrestacionesPermitidas', 'id_prestacion'),
			'MiCategoriaPrestacion' => array(self::BELONGS_TO, 'CategoriaPrestaciones', 'id_categoriaprestacion'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'codigo' => 'Codigo',
			'tema' => 'Tema',
			'descripcion' => 'Descripcion',
			'precio' => 'Precio',
			'dias_uti' => 'Dias Uti',
			'dias_sala' => 'Dias Sala',
			'dias_totales' => 'Dias Totales',
			'dias_max' => 'Dias Max',
			'maternidad' => 'Maternidad',
			'id_categoriaprestacion' => 'Id Categoriaprestacion',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('codigo',$this->codigo,true);
		$criteria->compare('tema',$this->tema,true);
		$criteria->compare('descripcion',$this->descripcion,true);
		$criteria->compare('precio',$this->precio,true);
		$criteria->compare('dias_uti',$this->dias_uti);
		$criteria->compare('dias_sala',$this->dias_sala);
		$criteria->compare('dias_totales',$this->dias_totales);
		$criteria->compare('dias_max',$this->dias_max);
		$criteria->compare('maternidad',$this->maternidad,true);
		$criteria->compare('id_categoriaprestacion',$this->id_categoriaprestacion);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Prestaciones the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
        
        public function rulesPrecioValido(){
         $reglas = "/^[0-9]{1,10}(\.[0-9]{1,2}){0,1}$/";  
         
          if (!(preg_match($reglas, $this->precio)))
              $this->addError('precio','Error!!. PRECIO INVALIDO. Solo se permiten cargar precios enteros o con decimales. $xx o $xxxxx.xx!!!'); 
          
          
        }
}
